package Chapter8;

import com.sun.glass.ui.View;
import org.browsermob.core.har.Har;
import org.browsermob.proxy.ProxyServer;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;

/**
 * Created by m.zabarylo on 10/10/2014.
 */
public class BrowserMobDemo {
    public static void main(String[] args) throws Exception
    {
        //start the browsermob proxy
        ProxyServer server = new ProxyServer(9090);
        server.start();
        //get the selenium proxy object
        Proxy proxy = server.seleniumProxy();
        //Configure desired capability for using Proxy Server
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        //start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);
        //create new HAR with the label "bmiCalculator"
        server.newHar("bmiCalculator");
        
        //open the bmi calc
        driver.get("http://dl.dropbox.com/u/55228056/bmicalculator.html");
        WebElement height = driver.findElement(By.name("heightCMS"));
        height.sendKeys("181");

        WebElement weight = driver.findElement(By.name("weightKg"));
        weight.sendKeys("80");

        WebElement calculateButton = driver.findElement(By.id("Calculate"));
        calculateButton.click();
        Thread.sleep(5000);

        //Get the HAR data
        Har har = server.getHar();
        //Write the HAR Data in File
        File harFile = new File("D:\\bmiCalculator.har");
        har.writeTo(harFile);

        //stop the BrowserMob Proxy
        server.stop();
        //close the driver
        driver.quit();

    }
}
